package com.lyf.scm.admin.domain.remote.bigdata.facade;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.lyf.scm.admin.domain.remote.basedata.dto.AreaDTO;
import com.lyf.scm.admin.domain.remote.basedata.dto.AreaLevelDTO;
import com.lyf.scm.admin.domain.remote.basedata.facade.BaseDataFacade;
import com.lyf.scm.admin.domain.remote.bigdata.BlOlapRemote;
import com.lyf.scm.admin.domain.remote.bigdata.dto.LayeredSkuCodePageDTO;
import com.lyf.scm.admin.domain.remote.bigdata.dto.ProductListQueryDTO;
import com.lyf.scm.admin.domain.remote.bigdata.dto.StockInAndOutDTO;
import com.lyf.scm.admin.domain.remote.bigdata.dto.WarehouseReceiveQtyDTO;
import com.lyf.scm.common.constants.CommonConstants;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.BaseSearchDTO;
import com.lyf.scm.common.model.SalesPlanSelectDTO;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author 李兵兵
 * @date 2020/09/25
 */
@Service
@Slf4j
public class BlOlapFacade {
    @Resource
    private BlOlapRemote blOlapRemote;
    @Resource
    private BaseDataFacade baseDataFacade;

    private static final Integer PAGE_NUM = 1;

    private static final Integer PAGE_SIZE = 100;

    public LayeredSkuCodePageDTO getProductList(ProductListQueryDTO productListQueryDTO){
        log.info("根据分层查询商品入参:productListQueryDTO:{}", JSON.toJSONString(productListQueryDTO));
        Response<LayeredSkuCodePageDTO> response = blOlapRemote.getProductList(productListQueryDTO);
        if(!ResponseMsg.SUCCESS.getCode().equals(response.getCode())){
            throw new RomeException(ResponseMsg.EXCEPTION_2006.getCode(),ResponseMsg.EXCEPTION_2006.getMsg());
        }
        return response.getData();
    }

    public List<WarehouseReceiveQtyDTO> getStockInAndOut(StockInAndOutDTO stockInAndOutDTO){
        log.info("查询实际到货量入参:stockInAndOutDTO:{}", JSON.toJSONString(stockInAndOutDTO));
        Response<List<WarehouseReceiveQtyDTO>> response = blOlapRemote.getStockInAndOut(stockInAndOutDTO);
        if(!ResponseMsg.SUCCESS.getCode().equals(response.getCode())){
            throw new RomeException(ResponseMsg.EXCEPTION_2006.getCode(),ResponseMsg.EXCEPTION_2006.getMsg());
        }
        return response.getData();
    }
    public List<WarehouseReceiveQtyDTO> getStockIn(StockInAndOutDTO stockInAndOutDTO){
        log.info("查询实际到货量入参:stockInAndOutDTO:{}", JSON.toJSONString(stockInAndOutDTO));
        Response<List<WarehouseReceiveQtyDTO>> response = blOlapRemote.getStockIn(stockInAndOutDTO);
        if(!ResponseMsg.SUCCESS.getCode().equals(response.getCode())){
            throw new RomeException(ResponseMsg.EXCEPTION_2006.getCode(),ResponseMsg.EXCEPTION_2006.getMsg());
        }
        return response.getData();
    }

    /**
     * 获取分层参数
     *
     * @param baseSearch
     */
    public void getLayeredData(BaseSearchDTO baseSearch) {
        //判断是否入参分层参数
            if (CollectionUtils.isNotEmpty(baseSearch.getLayeredCodeList())) {
            if (CollectionUtils.isNotEmpty(baseSearch.getChannelCodeList())) {
                List<LayeredSkuCodePageDTO> layeredSkuList = new ArrayList<>();
                String startDate = DateUtil.formatDate(new Date());
                //String startDate = "2020-10-08";
                if (CollectionUtils.isEmpty(baseSearch.getProvinceCodeList()) && CollectionUtils.isEmpty(baseSearch.getCityCodeList())
                        && CollectionUtils.isEmpty(baseSearch.getCountyCodeList()) && CollectionUtils.isEmpty(baseSearch.getStoreCodeList())) {
                    baseSearch.getChannelCodeList().forEach(channel -> {
                        ProductListQueryDTO productLista = new ProductListQueryDTO();
                        productLista.setChannel(channel);
                        productLista.setRankType(baseSearch.getRandType());
                        productLista.setStartTime(startDate);
                        productLista.setPageNum(PAGE_NUM);
                        productLista.setPageSize(PAGE_SIZE);
                        productLista.setLayeredType(baseSearch.getLayeredCodeList());
                        layeredSkuList.add(getProductList(productLista));
                    });
                } else {
                    baseSearch.getChannelCodeList().forEach(channel -> {
                        ProductListQueryDTO productLista = new ProductListQueryDTO();
                        productLista.setChannel(channel);
                        productLista.setRankType(baseSearch.getRandType());
                        productLista.setStartTime(startDate);
                        productLista.setPageNum(PAGE_NUM);
                        productLista.setPageSize(PAGE_SIZE);
                        productLista.setLayeredType(baseSearch.getLayeredCodeList());
                        layeredSkuList.add(getProductList(productLista));

                        if (CollectionUtils.isNotEmpty(baseSearch.getProvinceCodeList())) {
                            List<AreaDTO> areaList = baseDataFacade.listAreaInfoByCode(baseSearch.getProvinceCodeList());
                            areaList.forEach(pro -> {
                                ProductListQueryDTO productList = new ProductListQueryDTO();
                                productList.setProvince(pro.getName());
                                productList.setChannel(channel);
                                productList.setRankType(baseSearch.getRandType());
                                productList.setStartTime(startDate);
                                productList.setPageNum(PAGE_NUM);
                                productList.setPageSize(PAGE_SIZE);
                                productList.setLayeredType(baseSearch.getLayeredCodeList());
                                layeredSkuList.add(getProductList(productList));
                            });
                        }
                        if (CollectionUtils.isNotEmpty(baseSearch.getCityCodeList())) {
                            List<AreaDTO> areaList = baseDataFacade.listAreaInfoByCode(baseSearch.getCityCodeList());
                            areaList.forEach(city -> {
                                ProductListQueryDTO productList = new ProductListQueryDTO();
                                productList.setCity(city.getName());
                                productList.setChannel(channel);
                                productList.setRankType(baseSearch.getRandType());
                                productList.setStartTime(startDate);
                                productList.setPageNum(PAGE_NUM);
                                productList.setPageSize(PAGE_SIZE);
                                productList.setLayeredType(baseSearch.getLayeredCodeList());
                                layeredSkuList.add(getProductList(productList));
                            });
                        }
                        if (CollectionUtils.isNotEmpty(baseSearch.getCountyCodeList())) {
                            List<AreaLevelDTO> areaLevelList = baseDataFacade.listRelationByArea(baseSearch.getCountyCodeList());
                            areaLevelList.forEach(con -> {
                                ProductListQueryDTO productList = new ProductListQueryDTO();
                                productList.setCity(con.getNameFullPath().split("-")[2]);
                                productList.setChannel(channel);
                                productList.setRankType(baseSearch.getRandType());
                                productList.setStartTime(startDate);
                                productList.setPageNum(PAGE_NUM);
                                productList.setPageSize(PAGE_SIZE);
                                productList.setLayeredType(baseSearch.getLayeredCodeList());
                                layeredSkuList.add(getProductList(productList));
                            });
                        }
                        if (CollectionUtils.isNotEmpty(baseSearch.getStoreCodeList())) {
                            baseSearch.getStoreCodeList().forEach(store -> {
                                ProductListQueryDTO productList = new ProductListQueryDTO();
                                productList.setStore(store);
                                productList.setChannel(channel);
                                productList.setRankType(baseSearch.getRandType());
                                productList.setStartTime(startDate);
                                productList.setPageNum(PAGE_NUM);
                                productList.setPageSize(PAGE_SIZE);
                                productList.setLayeredType(baseSearch.getLayeredCodeList());
                                layeredSkuList.add(getProductList(productList));
                            });
                        }
                    });
                }
                List<String> skuCodeList = new ArrayList<>();
                layeredSkuList.forEach(item -> item.getDataList().forEach(list -> skuCodeList.add(list.getProductCode())));
                if (skuCodeList.size() > 0) {
                    List<SalesPlanSelectDTO> dtoList = new ArrayList<>();
                    for (String skuCode : skuCodeList) {
                        SalesPlanSelectDTO dto = new SalesPlanSelectDTO();
                        dto.setCode(skuCode);
                        dto.setLevel(CommonConstants.CATEGORY_LEVEL_FOUR);
                        dtoList.add(dto);
                    }
                    baseSearch.setCategoryCodeList(dtoList);
                }


            }
        }
    }
}
